package com.java.websocket.web.controler;

import com.java.websocket.model.User;
import com.java.websocket.service.IUserService;
import com.java.websocket.vo.PageVo;
import com.java.websocket.vo.RestResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@RestController
@RequestMapping("/user")
@Api(tags = "用户相关接口")
public class UserController {


    @Autowired
    private IUserService userService;

    // 新增接口
    @PostMapping("add")
    @ApiOperation("添加用户")
    public RestResultVo<Long> add(@RequestBody User user) {
        Long add = userService.add(user);
        if (add > 0) {
            return RestResultVo.ok(add);
        }
        return RestResultVo.failed("添加失败");
    }

    // 修改接口
    @PostMapping("edit")
    @ApiOperation("修改用户")
    public RestResultVo<User> edit(@RequestBody User user) {
        User edit = userService.editByUserId(user);
        if (edit != null) {
            return RestResultVo.ok(edit);
        }
        return RestResultVo.failed();
    }

    // 删除接口
    @GetMapping("delete")
    @ApiOperation("删除用户")
    public RestResultVo<Integer> delete(String userId) {
        int deleteOne = userService.deleteOne(userId);
        return RestResultVo.ok(deleteOne);
    }

    // 详情接口
    @GetMapping("detail")
    @ApiOperation("用户详情")
    public RestResultVo<User> detail(String userId) {
        User user = userService.detail(userId);
        return RestResultVo.ok(user);
    }

    // 分页查询
    @GetMapping("page")
    @ApiOperation("分页查询")
    public RestResultVo<PageVo<User>> page(String userName, Integer pageNumber, Integer pageSize) {
        PageVo<User> page = userService.page(userName, pageNumber, pageSize);
        return RestResultVo.ok(page);
    }

}
